package com.googlecode.maurersmarbles;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;

public class Util {

	/**
	 * Creates a new card deck.
	 * @return a new card deck
	 */
	public static List<Card> createCardDeck() {
		List<Card> cardDeck = new ArrayList<Card>();
		for (int id : Card.CARD_IDS) {
			for (char suit : Card.CARD_SUITS) {
				if (id == Card.CARD_ID_4) {
					cardDeck.add(new Card(id, suit, -4));
				}
				else {
					cardDeck.add(new Card(id, suit));
				}
			}
		}
		return cardDeck;
	}

	/**
	 * Draws an empty polygon using the given corner points.
	 * @param canvas the canvas to draw the polygon in
	 * @param corners the corners of the polygon
	 * @param paint the paint to draw the polygon with
	 */
	public static void drawEmptyPolygon(Canvas canvas, Point[] corners,
			Paint paint) {
		if (corners != null && corners.length > 0) {
			for (int i = 0; i < corners.length - 1; i++) {
				canvas.drawLine(corners[i].x, corners[i].y, corners[i + 1].x,
						corners[i + 1].y, paint);
			}
			canvas.drawLine(corners[corners.length - 1].x,
					corners[corners.length - 1].y, corners[0].x, corners[0].y,
					paint);
		}
	}

	/**
	 * Draws an empty rectangle.
	 * @param canvas the canvas to draw the rectangle in
	 * @param startX the start x location of the rectangle
	 * @param startY the start y location of the rectangle
	 * @param width the width of the rectangle
	 * @param height the height of the rectangle
	 * @param paint the paint to draw the rectangle with
	 */
	public static void drawEmptyRectangle(Canvas canvas, int startX,
			int startY, int width, int height, Paint paint) {
		canvas.drawLine(startX, startY, startX + width, startY, paint);
		canvas.drawLine(startX + width, startY, startX + width,
				startY + height, paint);
		canvas.drawLine(startX + width, startY + height, startX, startY
				+ height, paint);
		canvas.drawLine(startX, startY + height, startX, startY, paint);
	}

	/**
	 * Draws an empty rectangle.
	 * @param canvas the canvas to draw the rectangle in
	 * @param rect the bounding rectangle
	 * @param paint the paint to draw the rectangle with
	 */
	public static void drawEmptyRectangle(Canvas canvas, Rect rect, Paint paint) {
		drawEmptyRectangle(canvas, rect.left, rect.top, rect.width(),
				rect.height(), paint);
	}

	/**
	 * Draws the given bitmap in the given canvas.
	 * @param canvas the canvas to draw the image in
	 * @param bitmap the image to draw
	 * @param x the start x location of the image
	 * @param y the start y location of the image
	 * @param width the width of the image
	 * @param height the height of the image
	 */
	public static void drawImage(Canvas canvas, Bitmap bitmap, int x, int y,
			int width, int height) {

		Rect srcRect = new Rect();
		srcRect.left = 0;
		srcRect.top = 0;
		srcRect.right = bitmap.getWidth();
		srcRect.bottom = bitmap.getHeight();

		Rect dstRect = new Rect();
		dstRect.left = x;
		dstRect.top = y;
		dstRect.right = x + width;
		dstRect.bottom = y + height;

		canvas.drawBitmap(bitmap, srcRect, dstRect, null);

	}

	/**
	 * Draws the given bitmap in the given canvas.
	 * @param canvas the canvas to draw the image in
	 * @param bitmap the image to draw
	 * @param point the start x/y location of the image
	 * @param width the width of the image
	 * @param height the height of the image
	 */
	public static void drawImage(Canvas canvas, Bitmap bitmap, Point point,
			int width, int height) {
		drawImage(canvas, bitmap, point.x, point.y, width, height);
	}

	/**
	 * Returns a point that is linearly interpolated between point1 and point2
	 * by the given weight.
	 * @param point1 the first point
	 * @param point2 the second point
	 * @param weight the weight between point1 and point2 (0.0 - 1.0)
	 * @return a point that is linearly interpolated between point1 and point2
	 *         by the given weight
	 */
	public static Point interpolate(Point point1, Point point2, double weight) {
		int x = (int) (point1.x + (point2.x - point1.x) * weight);
		int y = (int) (point1.y + (point2.y - point1.y) * weight);
		return new Point(x, y);
	}

	/**
	 * Shuffles the given list of cards.
	 * @param cards the cards to shuffle
	 */
	public static void shuffleCards(List<Card> cards) {
		Collections.shuffle(cards);
	}

}
